package com.hsjk.szwj.module.train.controller.admin;

import com.hsjk.szwj.framework.common.pojo.CommonResult;
import com.hsjk.szwj.framework.common.pojo.PageResult;
import com.hsjk.szwj.module.train.controller.admin.vo.AdminCreateProductSkuReqVO;
import com.hsjk.szwj.module.train.controller.admin.vo.AdminProductSkuDetailRespVO;
import com.hsjk.szwj.module.train.controller.admin.vo.AdminProductSkuPageReqVO;
import com.hsjk.szwj.module.train.controller.admin.vo.AdminProductSkuPageRespVO;
import com.hsjk.szwj.module.train.controller.admin.vo.AdminUpdateProductSkuReqVO;
import com.hsjk.szwj.module.train.convert.ProductConvert;
import com.hsjk.szwj.module.train.dal.dataobject.product.ProductSkuDO;
import com.hsjk.szwj.module.train.service.IProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

import static com.hsjk.szwj.framework.common.pojo.CommonResult.success;

/**
 * @author chenjiawei
 * @desc
 * @Date 2023/09/12 00:27
 **/
@Tag(name = "后台管理 - 商品Sku")
@RestController
@RequestMapping("/train/product-sku")
@Validated
@Slf4j
public class AdminProductSkuController {
    @Resource
    private IProductService productService;

    @GetMapping("/page")
    @Operation(summary = "/获取商品Sku分页列表")
    @PreAuthorize("@ss.hasPermission('train:product-sku:query')")
    public CommonResult<PageResult<AdminProductSkuPageRespVO>> getProductSkuPageList(@Valid AdminProductSkuPageReqVO reqVO) {
        return success(productService.getProductSkuPageList(reqVO));
    }

    @GetMapping("/get")
    @Operation(summary = "/获取商品Sku详情")
    @PreAuthorize("@ss.hasPermission('train:product-sku:query')")
    public CommonResult<AdminProductSkuDetailRespVO> getProductSkuById(Long id) {
        ProductSkuDO skuDO = productService.getProductSkuById(id);
        return success(ProductConvert.INSTANCE.convert(skuDO));
    }

    @PostMapping("/create")
    @Operation(summary = "创建商品Sku")
    @PreAuthorize("@ss.hasPermission('train:product-sku:create')")
    public CommonResult<Boolean> createProductSku(@RequestBody @Valid AdminCreateProductSkuReqVO reqVO) {
        return success(productService.createProductSku(reqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新商品Sku")
    @PreAuthorize("@ss.hasPermission('train:product-sku:update')")
    public CommonResult<Boolean> updateProductSku(@RequestBody @Valid AdminUpdateProductSkuReqVO reqVO) {
        return success(productService.updateProductSku(reqVO));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商品Sku")
    @Parameter(name = "id", description = "id", required = true)
    public CommonResult<Boolean> deleteProductSku(@RequestParam("id") Long id) {
        return success(productService.deleteProductSku(id));
    }
}
